const jwt = require('jsonwebtoken')

const jwtKey = 'hfs78dfyds78f7dstyf78dstf78dstf' // token生成的密匙，根据自己需求定义

const jwtSign = (data) => { // token生成函数，有效时间为一个小时
    const token = jwt.sign(data, jwtKey, { expiresIn: 1 * 60 })//1分钟
    return token
}

const jwtCheck = (req, res, next) => { // token验证函数
    // console.log(req.headers)
    const token = req.headers.authorization
    console.log(token)
    jwt.verify(token, jwtKey, (err, data) => {
        if (err) {
            console.log("wuxiao")
            res.send({
                code: 403,
                msg: 'token无效'
            })
        } else {
            console.log("youxiao")
            req.jwtInfo = data
            next()
        }
    })
}

module.exports = {
    jwtSign,
    jwtCheck
}
